﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Simple.Data;

namespace Model.Course
{
    public class Attachment
    {
        public enum OwnerType { Task, Lecture, SolvedTask };

        protected Int32 id;
        protected String fileName;
        protected String filePath;
        protected OwnerType ownerType;
        protected Int32 ownerId;

        protected Attachment(Int32 id, String fileName, String filePath)
        {
            this.id = id;
            this.fileName = fileName;
            this.filePath = filePath;
        }

        protected Attachment(Int32 id, String fileName, String filePath, OwnerType ownerType, Int32 ownerId)
        {
            this.id = id;
            this.fileName = fileName;
            this.filePath = filePath;
            this.ownerType = ownerType;
            this.ownerId = ownerId;
        }

        public static Attachment create(String fileName, String filePath)
        {
            var att = Database.Open().Attachment.Insert(FileName: fileName,
                        FilePath: filePath, Task: null, Lecture: null, SolvedTask: null);

            return new Attachment(att.id, fileName, filePath);
        }

        public static Attachment[] getAttachments(OwnerType ownerType, Int32 ownerId)
        {
            var att = (dynamic) null;
            int count = 0;

            switch (ownerType)
            {
                case OwnerType.Lecture:
                    att = Database.Open().Attachment.FindAllByLecturer(ownerId);
                    count = Database.Open().Attachment.GetCountByLecturer(ownerId);
                    break;

                case OwnerType.Task:
                    att = Database.Open().Attachment.FindAllByTask(ownerId);
                    count = Database.Open().Attachment.GetCountByTask(ownerId);
                    break;

                case OwnerType.SolvedTask:
                    att = Database.Open().Attachment.FindAllBySolvedTask(ownerId);
                    count = Database.Open().Attachment.GetCountBySolvedTask(ownerId);
                    break;
            }

            Attachment[] attachments = new Attachment[count];

            int i = 0;
            foreach (var a in att)
            {
                attachments[i++] = new Attachment(a.Id, a.fileName, a.filePath, ownerType, ownerId);
            }

            return attachments;
        }

        public int getId()
        {
            return id;
        }

        public String getFileName()
        {
            return fileName;
        }

        public String getFilePath()
        {
            return filePath;
        }

        public OwnerType getOwnerType()
        {
            return ownerType;
        }

        public int getOwnerId()
        {
            return ownerId;
        }

        public void setOwnerId(OwnerType ot, int ownerId)
        {
            ownerType = ot;
            this.ownerId = ownerId;

            switch (ot)
            {
                case OwnerType.Lecture:
                    Database.Open().Attachment.UpdateById(id: id, task: null, solvedTask: null, lecturer: ownerId);
                    break;
                case OwnerType.Task:
                    Database.Open().Attachment.UpdateById(id: id, task: ownerId, solvedTask: null, lecturer: null);
                    break;
                case OwnerType.SolvedTask:
                    Database.Open().Attachment.UpdateById(id: id, task: null, solvedTask: ownerId, lecturer: null);
                    break;
            }
        }
    }
}
